﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PingTest.Service.Interfaces;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;

namespace PingTest.Service
{
    public class SitePing : ISitePingTest
    {
        public string PingSite(Dto.SiteInfo siteInfo)
        {
            try
            {
                Ping pingSite = new Ping();
                var siteUrl = Regex.Replace(siteInfo.SiteUrl, @"http(s?)://", string.Empty);

                if (Regex.IsMatch(siteUrl, @"www\.\S+\.\S+", RegexOptions.Singleline | RegexOptions.IgnoreCase))
                {
                    PingReply reply = pingSite.Send(siteUrl);
                    return String.Concat("#", PingResponseColour(reply));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return "#ff00ff";

                //       {
            
        }

        private static string PingResponseColour(PingReply reply)
        {
            switch (reply.Status)
            {
                case IPStatus.BadDestination:
                    break;
                case IPStatus.BadHeader:
                    break;
                case IPStatus.BadOption:
                    break;
                case IPStatus.BadRoute:
                    break;
                case IPStatus.DestinationHostUnreachable:
                    break;
                case IPStatus.DestinationNetworkUnreachable:
                    break;
                case IPStatus.DestinationPortUnreachable:
                    break;
                case IPStatus.DestinationProhibited:
                    break;
                //case IPStatus.DestinationProtocolUnreachable:
                //    break;
                case IPStatus.DestinationScopeMismatch:
                    break;
                case IPStatus.DestinationUnreachable:
                    break;
                case IPStatus.HardwareError:
                    break;
                case IPStatus.IcmpError:
                    break;
                case IPStatus.NoResources:
                    break;
                case IPStatus.PacketTooBig:
                    break;
                case IPStatus.ParameterProblem:
                    break;
                case IPStatus.SourceQuench:
                    break;
                case IPStatus.Success:
                    return "40c88b";
                case IPStatus.TimeExceeded:
                    break;
                case IPStatus.TimedOut:
                    break;
                case IPStatus.TtlExpired:
                    break;
                case IPStatus.TtlReassemblyTimeExceeded:
                    break;
                case IPStatus.Unknown:
                    break;
                case IPStatus.UnrecognizedNextHeader:
                    break;
                default:
                    break;
            }

            return "#ff00ff";
        }
    }
}
